const express = require('express')
const fs = require('fs')
const app = express()
const request = require('request')
const net = require('net')
const http = require('http')
//const Server = require('./src/api/server');
//const fs = require('fs')
let HOST_OTHER = '110.76.30.67'

let HOST = '157.255.28.121'
let HOST2 = '157.255.139.128'
let PORT = '443'
console.log('test')

const axios = require('axios')

class Server{
    axios(method,url,params){
      return new Promise((resolve,reject) =>{
        if(typeof params != 'object'){
          params = {}
        }
        let _option = {
          method,
          url,
          baseURL:'http://www.historychina.net',//envconfig.baseURL,
          timeout:30000,
          params:null,
          data:null,
          headers:null,
          withCredentials:true,// send request with Cookie
          validateStatus:(status) =>{
            return status >=200 && status <300
          },
          ...params
        }
        axios.request(_option).then(res =>{
          resolve( typeof res === 'object' ? res.data:JSON.parse(res.data))
        },error => {
          if(error.response){
            reject(error.response.data)
          }else{
            reject(error)
          }
        })
      })
    }
  }

let service  = new Server()

let getRequest = async function (){
   let res = await service.axios('GET','/qsbk/rw/index_1.shtml');
   console.log(res)
}
getRequest();

// for(let i = 1; i< 1000;i++){
//     http.get('http://www.historychina.net/qsbk/rw/index_' +i+'.shtml',function(req,res){
//         req.on('data',function(data){
//             //console.log('data : ' +data)
//             fs.appendFileSync('./test.txt',data,function(err){
//                 console.log('write file error' + err)
//             })
//         })
//         req.on('end',function(){
//             console.log('end')
//         })
//     })
// }


// require('whatwg-fetch')
// fetch('http://www.historychina.net/qsbk/rw/index_5.shtml',{
//     method:'GET'
// }).then(
//     (res) =>{
//         console.log(res.text())
//     }
// )

// app.get('http://www.historychina.net/qsbk/rw/index_5.shtml',function(req,res){
//     console.log(res.toString())
// })

// let client = new net.Socket()
// client.connect(PORT,HOST2,function(){
//     console.log('connect...')
//     client.write('data')
// })
// client.on('data',function(data){

//     console.log(data.toString())
//     client.destroy()
// })

// let req = http.request({
//     //hostname:'http://www.historychina.net/qsbk/rw/index_5.shtml',
//     path:'',
//     method:'GET'
// },function(res){
//     console.log(res);
// })
// req.on('error',function(e){
//     console.log('error : ' +e);
// })

